<?php

/**
 * ECSHOP 搜索程序
 * ============================================================================
 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: sxc_shop $
 * $Id: search.php 16908 2009-12-18 08:25:08Z sxc_shop $
*/

define('IN_ECS', true);
define('ECS_ADMIN', true);
if (empty($_GET['encode']))
{
	$string = array_merge($_GET, $_POST);
	if (get_magic_quotes_gpc())
	{
		require(dirname(__FILE__) . '/../includes/lib_base.php');
		//require(dirname(__FILE__) . '/includes/lib_common.php');

		$string = stripslashes_deep($string);
	}
	$string['search_encode_time'] = time();
	$string = str_replace('+', '%2b', base64_encode(serialize($string)));

	header("Location: search.php?encode=$string\n");

	exit;
}
else
{
	$string = base64_decode(trim($_GET['encode']));
	if ($string !== false)
	{
		$string = unserialize($string);
		if ($string !== false)
		{
			/* 用户在重定向的情况下当作一次访问 */
			if (!empty($string['search_encode_time']))
			{
				if (time() > $string['search_encode_time'] + 2)
				{
					define('INGORE_VISIT_STATS', true);
				}
			}
			else
			{
				define('INGORE_VISIT_STATS', true);
			}
		}
		else
		{
			$string = array();
		}
	}
	else
	{
		$string = array();
	}
}

require(dirname(__FILE__) . '/includes/init.php');

$_REQUEST = array_merge($_REQUEST, addslashes_deep($string));

$_REQUEST['act'] = !empty($_REQUEST['act']) ? trim($_REQUEST['act']) : '';
//if ($_SESSION['user_id'] > 0)
//{
//	$smarty->assign('user_name', $_SESSION['user_name']);
//
//}

/*------------------------------------------------------ */
//-- 搜索结果
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'get_all')
{
	$key   = !empty($_REQUEST['key'])   ? trim($_REQUEST['key'])	 : '';
 	$pageNo = !empty($_REQUEST['pageNo']) ? intval($_REQUEST['pageNo']) : 1;
	$pageSize=!empty($_REQUEST['pageSize'])?intval($_REQUEST['pageSize']) :10;
	$type =!empty($_REQUEST['type'])?intval($_REQUEST['type']) :0;            			
	
	if ($type==0)
	{			
		$result['sou']['goods'] = search_goods($key, 1, 3);
	}

	$result['sou']['news'] = search_news($key, $type, $pageNo, $pageSize, 1);
	$result['sou']['count'] = 512;	
		
	echo json_encode($result);	
}

else if ($_REQUEST['act'] == 'get_goods')
{
	$key   = !empty($_REQUEST['key'])   ? trim($_REQUEST['key'])	 : '';
 	$pageNo = !empty($_REQUEST['pageNo']) ? intval($_REQUEST['pageNo']) : 1;
	$pageSize=!empty($_REQUEST['pageSize'])?intval($_REQUEST['pageSize']) :10;
	$type =!empty($_REQUEST['type'])?intval($_REQUEST['type']) :0;            			

	$result['sou'] = search_goods($key, $pageNo, $pageSize);
	
	echo json_encode($result);	
}

else if ($_REQUEST['act'] == 'get_news')
{
	$key   = !empty($_REQUEST['key'])   ? trim($_REQUEST['key'])	 : '';
 	$pageNo = !empty($_REQUEST['pageNo']) ? intval($_REQUEST['pageNo']) : 1;
	$pageSize=!empty($_REQUEST['pageSize'])?intval($_REQUEST['pageSize']) :10;
	$type =!empty($_REQUEST['type'])?intval($_REQUEST['type']) :0;            			

	$result['sou'] = search_news($key, $type, $pageNo, $pageSize);
	
	echo json_encode($result);	
}

function search_goods($key, $pageNo, $pageSize)
{
	/* 初始化搜索条件 */
	$keywords  = '';
	if (!empty($key))
	{
		$arr = array();
		if (stristr($key, ' AND ') !== false)
		{
			/* 检查关键字中是否有AND，如果存在就是并 */
			$arr		= explode('AND', $key);
			$operator   = " AND ";
		}
		elseif (stristr($key, ' OR ') !== false)
		{
			/* 检查关键字中是否有OR，如果存在就是或 */
			$arr		= explode('OR', $key);
			$operator   = " OR ";
		}
		elseif (stristr($key, ' + ') !== false)
		{
			/* 检查关键字中是否有加号，如果存在就是或 */
			$arr		= explode('+', $key);
			$operator   = " OR ";
		}
		else
		{
			/* 检查关键字中是否有空格，如果存在就是并 */
			$arr		= explode(' ', $key);
			$operator   = " AND ";
		}

		$keywords = 'AND (';
		foreach ($arr AS $key => $val)
		{
			if ($key > 0 && $key < count($arr) && count($arr) > 1)
			{
				$keywords .= $operator;
			}
			$val		= mysql_like_quote(trim($val));
			$keywords  .= "(goods_name LIKE '%$val%' OR keywords LIKE '%$val%' )";

		}
		$keywords .= ')';
	}

	/* 查询商品 */
    $sql = "SELECT g.goods_id, g.goods_name, g.goods_img, g.shop_price as price, g.level, g.intelligent, g.property " .
            "FROM " . $GLOBALS['ecs']->table('goods') . " AS g " .
			"WHERE g.is_delete = 0 AND g.is_on_sale = 1 AND g.is_alone_sale = 1 ".
			"AND (( 1 "  . $keywords . " )  ) " .
			"ORDER BY g.goods_id ";
            			
	$res = $GLOBALS['db']->SelectLimit($sql, $pageSize, ($pageNo-1)*$pageSize);
	
	$arr = array();
	while ($row = $GLOBALS['db']->FetchRow($res))
	{
		$goods = $row;
		$goods['level'] = '';		
		if ($row['level']>0)
		{
			$goods['level'] = $GLOBALS['db']->getOne('SELECT lable_title FROM '.$GLOBALS['ecs']->table('label')." WHERE label_id = ".$row['level']);
		}
		$goods['intelligent'] = '';		
		if ($row['intelligent']>0)
		{
			$goods['intelligent'] = $GLOBALS['db']->getOne('SELECT lable_title FROM '.$GLOBALS['ecs']->table('label')." WHERE label_id = ".$row['intelligent']);
		}
		$goods['property'] = '';
		if ($row['property']>0)
		{
			$goods['property'] = $GLOBALS['db']->getOne('SELECT lable_title FROM '.$GLOBALS['ecs']->table('label')." WHERE label_id = ".$row['property']);
		}				
		//校正图片地址
		$goods['goods_img'] = OSS_HOST.get_image_path($goods['goods_id'],$row['goods_img'],false,'goods');		

		$goods['good_count'] = 10;	
		$goods['price'] = (int)$row['price'];		
										
		array_push($arr,$goods);
	}
	
	return $arr;
}	
function search_news($key, $type, $pageNo, $pageSize, $store=0)
{
	/* 初始化搜索条件 */
	$keywords  = '';
	if (!empty($key))
	{
		$arr = array();
		if (stristr($key, ' AND ') !== false)
		{
			/* 检查关键字中是否有AND，如果存在就是并 */
			$arr		= explode('AND', $key);
			$operator   = " AND ";
		}
		elseif (stristr($key, ' OR ') !== false)
		{
			/* 检查关键字中是否有OR，如果存在就是或 */
			$arr		= explode('OR', $key);
			$operator   = " OR ";
		}
		elseif (stristr($key, ' + ') !== false)
		{
			/* 检查关键字中是否有加号，如果存在就是或 */
			$arr		= explode('+', $key);
			$operator   = " OR ";
		}
		else
		{
			/* 检查关键字中是否有空格，如果存在就是并 */
			$arr		= explode(' ', $key);
			$operator   = " AND ";
		}

		$keywords = 'AND (';
		foreach ($arr AS $key => $val)
		{
			if ($key > 0 && $key < count($arr) && count($arr) > 1)
			{
				$keywords .= $operator;
			}
			$val		= mysql_like_quote(trim($val));

			$keywords  .= "(a.title LIKE '%$val%' OR a.keywords LIKE '%$val%')";

			if ($store)
			{
				$GLOBALS['db']->autoReplace($GLOBALS['ecs']->table('keywords'), array('date' => local_date('Y-m-d'),
				'searchengine' => 'ecshop', 'keyword' => $val, 'count' => 1), array('count' => 1));
			}
		}
		$keywords .= ')';
	}
	
	/* 获取文章内容 */
	$sql = " select a.title,a.description,a.click_count,a.article_id,a.file_url as img_url,ac.cat_name,cr.copyright_type,ac.show_type from " . $GLOBALS['ecs']->table('article') . ' AS a LEFT JOIN '
				 . $GLOBALS['ecs']->table('article_cat') . ' AS ac ON a.cat_id= ac.cat_id ' 
				 .' LEFT JOIN '. $GLOBALS['ecs']->table('copyright') . ' AS cr ON a.copyright_id = cr.copyright_id ' 
				."WHERE (( 1 "  . $keywords . " ) ) " ;
					
	if ($type == 1)
	{//评测
		$sql .= " AND (ac.show_type = 1 OR ac.show_type = 2) ";			
	}
	else if ($type == 2)
	{//视频
		$sql .= " AND (ac.show_type = 3) ";					
	}
	else if ($type == 3)
	{//资讯
		$sql .= " AND (ac.show_type = 4 OR ac.show_type = 5) ";					
	}	
	$sql .= " ORDER BY a.add_time desc ";
	$res = $GLOBALS['db']->SelectLimit($sql, $pageSize, ($pageNo-1)*$pageSize);
		
	$arr = array();
	while ($row = $GLOBALS['db']->FetchRow($res))
	{
//		var_dump($sql);
		$article = $row;
		//文章头图
		$article['img_url'] = OSS_HOST.get_image_path($article['article_id'],$article['img_url'],false,'artile');	
		$article['copyright_type'] == 1 ? $article['is_original'] = 1 : $article['is_original'] = 0;							
		array_push($arr,$article);
	}
	
	return $arr;
}
?>